angular.module('module.template')
    .component('messageComponent', {
        template: '<div ng-if="vm.messageVisible" ng-include="vm.path">',
        bindings: {
            messageVisible: '<',
            message: '<',
            btn: '<',
        },
        require: {},

        controller: ('MessageComponentController', MessageComponentController),
        controllerAs: 'vm'

    })
    .component('loginComponent', {
        template: '<div ng-if="vm.loginVisible" ng-include="vm.loginpath">',
        bindings: {
            loginVisible: '<',
            message: '<',
            closebtn: '&',
            gopassword: '&'
        },
        require: {},

        controller: ('LoginComponentController', LoginComponentController),
        controllerAs: 'vm'

    })
    .component('registerComponent', {
        template: '<div ng-if="vm.registerVisible" ng-include="vm.registerpath">',
        bindings: {
            registerVisible: '<',
            message: '<',
            closebtnt: '&',
        },
        require: {},

        controller: ('RegisterComponentController', RegisterComponentController),
        controllerAs: 'vm'

    })
MessageComponentController.$inject = ["AuthService"];
function MessageComponentController(AuthService) {
    var vm = this;
    vm.path = template_viewurl + "modeltpl.html";

    vm.cancelbtn = cancelbtn;
    vm.surebtn = surebtn;
    function cancelbtn() {
        vm.messageVisible = false;
        return;
    }

    function surebtn() {
        vm.messageVisible = false;
        return;
    }

}
LoginComponentController.$inject = ['AuthService', '$localStorage', '$rootScope', 'AUTH_EVENTS'];
function LoginComponentController(AuthService, $localStorage, $rootScope, AUTH_EVENTS) {

    var vm = this;
    vm.wornWordT = [false, false, false];
    vm.loginpath = template_viewurl + "login.html";
    vm.gosign = gosign;//登录
    vm.automaticLogin = automaticLogin;//自动登录
    function automaticLogin() {

    }

    function gosign() {
        var domlist = $("#logininput").find("input");
        for (var i = 0; i < domlist.length; i++) {
            if (!domlist[i].value) {
                vm.wornWordT[i] = true;
                return;
            }
        }
        var loginbody = {
            type:'',
            number:vm.body,
            password:hex_hmac_md5("huangxiaofang", vm.password)
        };
        if(checkEmail(vm.body)){
            loginbody.type=0;
        }else if(checkphone(vm.body)){
            loginbody.type=2;
        }else{
            loginbody.type=1;
        }

        //if (checkphone(vm.phone) != true) {
        //    vm.wornWordT[0] = true;
        //    return;
        //}
        var automatic = document.getElementById("automatic");
        var tips = automatic.checked;

        console.log(loginbody);
        AuthService.getRegister(loginbody, function (suc) {
            $localStorage.userid = suc.userid;
            $rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
        }, function (error) {
            console.log(error);
            var loginerror = error.data.msg;
            for (var i = 0; i < domlist.length; i++) {
                vm.wornWordT[i] = false;
            }
            $("#passverification").after('<label class="register-error"><span>' + loginerror + '</span></label>');
        })

    }
}

RegisterComponentController.$inject = ["AuthService", "$state", "$localStorage", "$rootScope", "AUTH_EVENTS"];
function RegisterComponentController(AuthService, $state, $localStorage, $rootScope, AUTH_EVENTS) {
    var vm = this;
    vm.registerpath = template_viewurl + "register.html";
    vm.wornWord = [false, false, false];

    vm.goRegister = goRegister;
    function goRegister() {
        vm.wornWord = [false, false, false];
        var domlist = $("#formid").find("input");
        for (var i = 0; i < domlist.length; i++) {
            if (!domlist[i].value) {
                vm.wornWord[i] = true;
                return;
            }
        }
        if (checkphone(vm.phone) != true) {
            vm.wornWord[1] = true;
            return;
        }
        var signbody = {
            name: vm.name,
            phone: vm.phone,
        };
        AuthService.sign(signbody, function (success) {
            var registerdata = {
                userid: success.userid,
                password: hex_hmac_md5("huangxiaofang", vm.password)
            };
            AuthService.register(registerdata, function (sucData) {
                $localStorage.userid = sucData.userid;
                $rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
            })

        }, function (error) {
            for (var i = 0; i < domlist.length; i++) {
                vm.wornWord[i] = false;
            }
            $("#verification").after('<label class="register-error"><span>该手机号已注册</span></label>');
        })
    }
}

